package com.kernel.net;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

/****************************************
 * @name UDPClient
 * @description
 * @author 黄彬恒
 * @since 2018/8/24
 ***************************************/
public class UDPClient {
    public static void main(String[] args) throws IOException {
        DatagramSocket ds;// 定义接收数据报的对象
        byte[] bytes = new byte[1024];
        DatagramPacket dp;
        ds = new DatagramSocket(9000);
        dp = new DatagramPacket(bytes, 1024);// 所有的信息使用bytes保存
        ds.receive(dp);
        String string = new String(dp.getData(), 0, dp.getLength()) + " from " + dp.getAddress().getHostAddress() + ": " + dp.getPort();
        System.out.println(string);
    }
}
